package com.venue.hjt.venueclass.ui

import android.support.v7.app.AppCompatActivity
import android.os.Bundle
import android.os.Handler
import android.util.Log
import android.view.WindowManager
import com.google.gson.Gson

import com.venue.hjt.venueclass.R
import com.venue.hjt.venueclass.adapter.ParentNewsAdapter
import com.venue.hjt.venueclass.bean.API
import com.venue.hjt.venueclass.bean.StudentNews
import com.venue.hjt.venueclass.db.CardDao
import com.venue.hjt.venueclass.utlis.Constant
import com.venue.hjt.venueclass.utlis.OkhttPost
import kotlinx.android.synthetic.main.activity_parent_news.*
import kotlinx.android.synthetic.main.activity_venue_complaint.*
import okhttp3.Call
import java.lang.Exception


class ParentNewsActivity : AppCompatActivity() {

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        supportActionBar!!.hide()
        window.setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN, WindowManager.LayoutParams.FLAG_FULLSCREEN)
        window.setSoftInputMode(
                WindowManager.LayoutParams.SOFT_INPUT_STATE_ALWAYS_HIDDEN)
        setContentView(R.layout.activity_parent_news)
        val student_no=intent.getStringExtra("student_no")
        OkhttPost.Okget(API.VENUESTUDNETMESSAGE + Constant.SNO+"/"+student_no, 2, object : OkhttPost.Callback{
            override fun onError(call: Call?, e: Exception?, id: Int) {
            }

            override fun onResponse(response: String?, id: Int) {
                val gson=Gson()
                val bean = gson.fromJson(response, StudentNews::class.java)
                if (bean.code==1){
                    val adapter= ParentNewsAdapter(this@ParentNewsActivity,bean.data)
                    lv_news.adapter=adapter
                    for (i in bean.data.indices){
                        if (bean.data[i].read_status == "0"){
                            OkhttPost.Okget(API.DELETENEWS + "?sno=" + Constant.SNO + "&id=" +bean.data[i].id,1,object :OkhttPost.Callback{
                                override fun onError(call: Call?, e: Exception?, id: Int) {
                                }

                                override fun onResponse(response: String?, id: Int) {
                                    Log.e("ATG",response)
                                    Log.e("ATG","已读")
                                }
                            })
                        }
                    }
                }
            }

        })
//        val queryReadNews = CardDao.queryReadNews(student_no)
//        val adapter= ParentNewsAdapter(this,queryReadNews)
//        lv_news.adapter=adapter
        Thread(Runnable {
            while (i > 0) {
                handler.sendEmptyMessage(1)
                if (i <= 0) {
                    break
                }
                try {
                    Thread.sleep(1000)
                } catch (e:InterruptedException) {
                    e.printStackTrace()
                }
                i--
            }
            handler.sendEmptyMessage(2)
        }).start()
        iv_parent_close.setOnClickListener({
            finish()
        })
    }
    internal var i = 30
    private var handler: Handler = object : Handler() {
        override fun handleMessage(msg: android.os.Message) {
            if (msg.what == 1) {
                tv_parent_close.text="还剩" + i + "关闭界面"
            } else {
                finish()
            }
        }
    }
}
